<?php
	namespace app\wechat\controller;

	use think\Controller;
	use app\wechat\model\Wechat;
	class Index extends Controller{
	public function index(){

		$echoStr = isset($_GET['echostr']) ? $_GET['echostr'] : '';
		$wechat = new Wechat();
		if ($wechat->checkSignature() && $echoStr) {
			return $echoStr;
		} else {			
			if(strtolower($postObj->MsgType) == 'event' && strtolower($postObj->Event) == 'subscribe'){
				$this->checkFocus();
			}
			$this->checkClickEvent();
		}

	}

	public function gettoken(){
		$res = file_get_contents('access_token.json');
		$result = json_decode($res, true);
		return $result['access_token'];
	}

	public function createMenu()
	{

		$wechat = new Wechat();
		$menuArr = array(
			array(
				'type' => 'click',
				'name' => urlencode('测试模板消息'),
				'key' => 'lanl_submenu_1'
				)            
			);
		$postArr = array(
			'button' => $menuArr
			);
		$postArr = urldecode(json_encode($postArr));
	// $access_token = curlGetAccessToken
		$url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$this->gettoken();
		return $wechat->http_request($url,$postArr);
	}

	public function checkClickEvent()
	{
		$wechat = new Wechat();
		$postObj = $wechat->sdkInit();
		$key = isset($postObj->EventKey) ? $postObj->EventKey : '';
		switch ($key) {
			case 'lanl_submenu_1':
			$this->postMessageByGroup();
			break;
		}
		
	}

	//发送模板消息
	public function sendTempmateMessage($data){
		$wechat = new Wechat();
		$access_token = $this->gettoken();
	// return $access_token;
		$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_token;
		$res = $wechat->http_request($url , $data);
		return json_decode($res , true);
	}

	//获取用户列表
	public function getUserList(){
		$wechat = new Wechat();      
		$url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=".$this->gettoken();
		$res = json_decode($wechat->http_request($url),true);
	// $userList =  array_merge($userList,$res['data']['openid']);
	// if(count($userList)>=10000){
	// 	$nextOpenid = $res['next_openid'];
	// 	$url += $url."&next_openid=".$nextOpenid;
	// 	$temp = json_decode($wechat->http_request($url),true);
	// }

	//$res['data']['openid']
		return $res['data']['openid'];
	}

	//获取用户基本信息
	//'nickname'昵称
	public function getUserInfo($openid){
		$wechat = new Wechat();
		$url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$this->gettoken()."&openid=$openid&lang=zh_CN";
		$res = json_decode($wechat->http_request($url),true);;
		return $res;
	}

	//群发通知
	public function postMessageByGroup(){
		$getUserList = $this->getUserList();
		foreach ($getUserList as $key => $value) {
			$count = count($getUserList);
			$openid = $getUserList[$key];
			$nickname = $this->getUserInfo($openid)['nickname'];
			$template = array(
				'touser' => $openid,
				'template_id' => "21SpMAvGes4mRkA_wVlLD2hpj5z2QLWkOlwRBM_Bzsc", //模版消息发送 消费品 - 消费品 - 购买成功通知  编号 OPENTM402058423
				'url' => "http://hintrinh.bj01.bdysite.com/tp5/public/index.php/wechat/index/test",
				'topcolor' => "#7B68EE",
				'data' => array(
					'first'=> array(
						'value' => urlencode("你好"),
						'color' => "#743A3A",
					),
					'nickname'    => array(
						'value' => urlencode($nickname),
						'color' => "#000093",
					),
					'time'     => array(
						'value' => date("Y-m-d H:i:s"),
						'color' => "#FF0000",
					),
					'count'     => array(
						'value' => $count,
						'color' => "#006000",
					),
					'end'     => array(
						'value' => urlencode("当前群发编号：").($key+1),
						'color' => "#000000",
					),
				)
			);
			$res = $this->sendTempmateMessage(urldecode(json_encode($template)));
			if($res['errcode'] != 0){
				return false;
			}
			
		}
		return true;

	}

	//

	public function test(){
		$wechat = new Wechat();
		$postObj = $wechat->sdkInit();
		return "<h1>这是详情页内容！测试用</h1>";
	
	}
	
	 
	}

